package org.study.swing.study01.component;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年11月14日
 */
public class OtherDemo {
    public static void main(String[] args) {
        testList1();
        // testSlider1();
        // testProgressBar1();
    }

    public static void testList1() {
        JFrame frame = new JFrame("示例：JList(列表框，用于显示一个列表)");
        Container container = frame.getContentPane();

        String[] options = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        JList list = new JList(options);
        container.add(list);
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                // 防止事件被重复处理
                if (!e.getValueIsAdjusting()) {
                    int index = list.getSelectedIndex();
                    String selectedValue = (String) list.getSelectedValue();
                    System.out.println("您选择了第" + (index + 1) + "项，其值为：" + selectedValue);
                }
            }
        });
        // 自定义列表渲染器
        list.setCellRenderer(new MyListCellRenderer());

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    static class MyListCellRenderer extends JLabel implements ListCellRenderer<Object> {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.setText(value.toString());
            if (isSelected) {
                // super.setBackground(list.getSelectionBackground());
                // super.setForeground(list.getSelectionForeground());
                super.setBackground(Color.PINK);
                super.setForeground(Color.RED);
            } else {
                super.setBackground(list.getBackground());
                super.setForeground(list.getForeground());
            }
            super.setEnabled(list.isEnabled());
            super.setFont(list.getFont());
            super.setOpaque(true);
            return this;
        }
    }

    public static void testSlider1() {
        JFrame frame = new JFrame("示例：JSlider(滑动条，用于选择一个数值)");
        Container container = frame.getContentPane();

        JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 25);
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int minimum = slider.getMinimum();
                int maximum = slider.getMaximum();
                int value = slider.getValue();
                System.out.println("最小值：" + minimum + "，最大值：" + maximum + "，当前值：" + value);
            }
        });
        container.add(slider, BorderLayout.CENTER);

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void testProgressBar1() {
        JFrame frame = new JFrame("示例：JProgressBar(进度条，用于显示一个进度)");
        Container container = frame.getContentPane();

        // JProgressBar progressBar = new JProgressBar(0, 0, 100);
        JProgressBar progressBar = new JProgressBar(0, 100);
        progressBar.setValue(25);
        container.add(progressBar, BorderLayout.CENTER);

        frame.setSize(600, 400);
        // 将窗口居中显示在屏幕上
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
